package org.oslims.client.uicomponents.editbox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.oslims.client.uicomponents.inputfields.DropDownInputField;
import org.oslims.client.uicomponents.inputfields.InputField;
import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.Parameter;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditParameterBox {

	final DialogBox editParameterBox = new DialogBox(false, true);
	final VerticalPanel editParameterBoxPanel = new VerticalPanel();

	protected Map<String, InputField> inputFields = new HashMap<String, InputField>();

	final HorizontalPanel buttonLine = new HorizontalPanel();
	final Button saveButton = new Button("Save");
	final Button cancelButton = new Button("Cancel");

	final TextInputField nameField = new TextInputField("name", "Name: ", "80px", "200px");
	final TextInputField unitField = new TextInputField("unit", "Unit: ", "80px", "200px");
	final DropDownInputField typeField;

	private Parameter parameter;

	String editedType;

	public EditParameterBox() {

		List<String> parameterType = new ArrayList<String>();

		parameterType.add("Boolean");
		parameterType.add("Date");
		parameterType.add("Number");
		parameterType.add("Text");

		typeField = new DropDownInputField("type", "Type: ", "80px", "200px", parameterType);

		editParameterBoxPanel.add(nameField);
		inputFields.put(nameField.getFieldName(), nameField);

		editParameterBoxPanel.add(unitField);
		inputFields.put(unitField.getFieldName(), unitField);

		editParameterBoxPanel.add(typeField);
		inputFields.put(typeField.getFieldName(), typeField);

		buttonLine.add(saveButton);
		buttonLine.add(cancelButton);

		editParameterBox.add(editParameterBoxPanel);

		// Add the save and cancel buttons
		editParameterBoxPanel.add(buttonLine);
	}

	public void hide() {
		editParameterBox.hide();
	}

	public void clear() {
		parameter = null;
		nameField.getInputField().setValue("");
		unitField.getInputField().setValue("");
		typeField.getInputBox().setSelectedIndex(-1);
	}

	public void showNew() {
		this.clear();
		this.clearAllErrors();
		editParameterBox.setText("Create new Parameter");
		editParameterBox.center();
	}

	public void showEdit(Parameter p) {
		this.clear();
		this.clearAllErrors();

		parameter = p;
		nameField.getInputField().setValue(p.getName());
		unitField.getInputField().setValue(p.getUnit());

		for (int i = 0; i < typeField.getInputBox().getItemCount(); i++) {

			if (typeField.getInputBox().getItemText(i).equals(p.getType())) {

				typeField.getInputBox().setSelectedIndex(i);
				break;
			}
		}

		editParameterBox.setText("Edit Parameter");
		editParameterBox.center();
	}

	public void showError(String fieldName, String message) {
		inputFields.get(fieldName).showError(message);
	}

	public void clearError(String fieldName) {
		inputFields.get(fieldName).clearError();
	}

	public void clearAllErrors() {
		for (InputField inputField : inputFields.values()) {
			inputField.clearError();
		}
	}

	public HasValue<String> getName() {
		return nameField.getInputField();
	}

	public HasValue<String> getUnit() {
		return unitField.getInputField();
	}

	public ListBox getType() {
		return typeField.getInputBox();
	}

	public HasClickHandlers getSaveButton() {
		return saveButton;
	}

	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

	public void setParameter(Parameter parameter) {
		this.parameter = parameter;
	}

	public Parameter getParameter() {
		return parameter;
	}
}
